<template>
	<!-- 
	swiper中的transfrom会使fixed失效,此时用height固定高度; 
	swiper中无法触发mescroll-mixins.js的onPageScroll和onReachBottom方法,只能用mescroll-uni,不能用mescroll-body
	-->
	<!-- ref动态生成: 字节跳动小程序编辑器不支持一个页面存在相同的ref (如不考虑字节跳动小程序可固定值为 ref="mescrollRef") -->
	<!-- top的高度等于悬浮菜单tabs的高度 -->
	<mescroll-uni
		:ref="'mescrollRef' + i"
		@upother="upother"
		@init="mescrollInit"
		:height="height"
		:down="downOption"
		@down="downCallback"
		:up="upOption"
		@up="upCallback"
		@emptyclick="emptyClick"
	>
		<!-- 数据列表 -->
		<u-gap height="20" bg-color="#F5F5F5"></u-gap>
		<view class="uni-block-list">
			<!-- <view class="order uni-flex-column" v-if="hasShow == 'none'">
				<u-empty text="您还没有相关的订单" mode="order"></u-empty>
			</view> -->
			<view class="list uni-flex-column">
				<view class="item uni-view" v-for="(order, index1) in goods" :key="index1" @click="goDetail(order)">
					<view class="head sm-font uni-flex-title uni-flex-row">
						<view class="">
							<text>订单编号：</text>
							<text>{{ order.order_no }}</text>
						</view>

						<text>{{ order.statustext }}</text>
					</view>
					<block v-for="(item, index2) in order.orderCommodity">
						<view class="body uni-flex-row" :key="index2">
							<view @click.stop="togoods(item.id, item.money_type)" class="img_box"><vimage class="" :src="item.master"></vimage></view>

							<view class="detail uni-flex-row ">
								<view class="uni-flex-column title">
									<text class="lg-font u-line-2">
										<text class="group_div" v-if="order.order_from == 1">{{ item.group }}人团</text>
										<text class="group_div" v-if="order.order_from == 2">秒杀</text>
										<text class="group_div" v-if="order.order_from == 3">预售</text>
										{{ item.name }}
									</text>
									<text class="skuvalue u-line-1">{{ item.pvs_value || '' }}</text>
									<!-- <text  class="skuvalue" v-if="item.attach_value">{{item.attach_value}}</text> -->
								</view>
								<view class="price">
									<text class="lg-font">
										<text class="sm-font" v-if="order.money_type == 1">&yen;&nbsp;</text>
										{{ item.fmoney }}x{{ item.count }}
									</text>
									<!-- <text class="num sm-font">&times;</text> -->
								</view>
							</view>
						</view>
						<!-- 	<view class="" v-if="item.attach_value">
							<text>{{ item.attach_value }}</text>
						</view> -->
						<view v-if="item.attachlist" v-for="(data, i) in item.attachlist" class="u-content-color u-flex u-row-between u-font-sm u-margin-bottom-20">
							<text>{{ data.attach_value }}</text>
							<text>&yen;&nbsp;{{ data.attach_price }}x{{ item.count }}</text>
						</view>
					</block>

					<view class="bottom uni-flex-row uni-flex-title" v-if="order.money_type == 1">
						<view class="left uni-flex-row sm-font">
							<text>运费：</text>
							<text class="price">&yen;&nbsp;{{ order.freight || '0.00' }}</text>
							<text>优惠：</text>
							<text class="price">&yen;&nbsp;{{ order.discount || '0.00' }}</text>
						</view>
					</view>
					<view class="bottom uni-flex-row uni-flex-title">
						<view class="right uni-flex-row sm-font">
							<text>
								共计
								<text class="number">{{ order.num }}</text>
								件商品,
							</text>
							<text>
								合计：
								<text class="price" v-if="order.money_type == 1">&yen;&nbsp;{{ order.allprice }}</text>
								<text class="price" v-else>{{ order.money }}</text>
							</text>
						</view>
					</view>
					<view class="button uni-flex-row">
						<block v-if="order.status == 1 && order.money_type == 1">
							<text class="cancel" @click.stop="cancelOrder(order.id)">取消订单</text>
							<text class="pay " @click.stop="topay(order)">立即支付</text>
						</block>
					</view>
				</view>
			</view>
		</view>
		<uni-login ref="Login" @callback="logincallback"></uni-login>
		<!-- <common v-if="showCommon"></common> -->
		<guesslike :page_id="'o' + i" @loadsucess="loadsucess" :ref="'guesslike' + i"></guesslike>
	</mescroll-uni>
</template>

<script>
import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js';
import MescrollMoreItemMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-more-item.js';
// import {apiGoods} from "@/api/mock.js"
import util from '@/common/util.js';
import http from '@/common/http.js';
import {mapState, mapMutations} from 'vuex';
export default {
	mixins: [MescrollMixin, MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
	data() {
		return {
			downOption: {
				auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
			},
			upOption: {
				auto: false, // 不自动加载
				page: {
					num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
					size: 10 // 每页数据的数量
				},
				noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
				empty: {
					tip: '暂无数据', // 提示
					btnText: '',
					icon: util.getimgurl('static/images/mescroll-empty.png', '1')
				}
			},
			goods: [], //列表数据
			hasShow: 'none',
			page: 1,
			size: 10,
			showCommon: false,
			resetUp: false
		};
	},
	props: {
		i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
		index: {
			// 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			type: Number,
			default() {
				return 0;
			}
		},
		tabs: {
			// 为了请求数据,演示用,可根据自己的项目判断是否要传
			type: Array,
			default() {
				return [];
			}
		},
		height: [Number, String] // mescroll的高度
	},

	computed: {
		...mapState({
			tradeSetting: state => state.tradeSetting
		})
	},
	methods: {
		upother() {
			console.log('loadothe');
		},
		loadsucess(i) {
			this.mescroll.endSuccess(i);
			// this.mescroll.showNoMore()
		},
		/*下拉刷新的回调 */
		downCallback() {
			// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
			// loadSwiper();
			// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			this.tabs[this.i].hasShow = false;
			this.resetUp = true;
			this.mescroll.resetUpScroll();
		},
		/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
		upCallback(page) {
			//联网加载数据
			let keyword = this.tabs[this.i].name;
			if (!this.tabs[this.i].hasShow) {
				this.getOrderList(page);
			} else {
				this.$refs['guesslike' + this.i].init();
			}

			// apiGoods(page.num, page.size, keyword).then(res=>{
			// 	//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
			// 	this.mescroll.endSuccess(res.list.length);
			// 	//设置列表数据
			// 	if(page.num == 1) this.goods = []; //如果是第一页需手动制空列表
			// 	this.goods=this.goods.concat(res.list); //追加新数据
			// }).catch(()=>{
			// 	//联网失败, 结束加载
			// 	this.mescroll.endErr();
			// })
		},
		//点击空布局按钮的回调
		emptyClick() {
			uni.showToast({
				title: '点击了按钮,具体逻辑自行实现'
			});
		},
		getOrderList(page) {
			let data = this.tabs[this.i];
			data.status = 'loading';
			const _this = this;

			const param = {
				page: page.num,
				size: page.size
			};
			if (data.state == 8 && !this.tradeSetting.order_evaluate) {
				this.mescroll.endSuccess(10);
				// this.mescroll.showEmpty()
				this.tabs[this.i].hasShow = true;
				this.mescroll.otherEmpty();
				this.$nextTick(function() {
					this.$refs['guesslike' + this.i].init();
				});

				return false;
			}

			if (data.state) {
				if (data.state == 12) {
					param.state = 3;
					param.order_type = 2;
				} else {
					param.state = data.state;
				}
			}

			http('/order/orderList', param, 'GET').then(
				res => {
					const json = res.data.msg;
					this.mescroll.endSuccess(10);
					if (json.data.length < 10) {
						this.tabs[this.i].hasShow = true;

						this.$nextTick(function() {
							this.$refs['guesslike' + this.i].init(_this.resetUp, json.data.length);
						});
					} else {
						this.tabs[this.i].hasShow = false;
					}

					if (json.total == 0) {
						this.mescroll.otherEmpty();
					} else {
						this.mescroll.removeotherEmpty();
					}
					let list = json.data;
					list.forEach(order => {
						order.num = 0;
						let status = order.status;
						if (order.money_type == 2) {
							status = order.mstatus;
						}
						let suppdata = {};

						if (order.orderCommodity[0] && order.orderCommodity[0].gc_id) {
							let gc_id = order.orderCommodity[0] && order.orderCommodity[0].gc_id.split(',');
							order.gc_id = gc_id[0];
							suppdata.groupstatus = order.orderCommodity[0].group_status;
						}

						order.statustext = util.getstatustext(status, order.order_type, order.money_type, suppdata);

						order.orderCommodity.forEach(item => {
							// if(order.attach&&order.attach.length>0){
							// 	item.attachlist=order.attach.filter(_=>_.commodity_id==item.id&&_.sku_id==item.sku_id)
							// }
							item.attachlist = [];
							if (order.attach && order.attach.length > 0 && item.attach_value && item.attach_value != '') {
								order.attach.forEach(a => {
									if (item.attach_value.indexOf(a.attach_value) > -1 && item.attachlist.findIndex(_ => _.attach_value == a.attach_value) == -1) {
										item.attachlist.push(a);
									}
								});
							}
							order.num += item.count;
							item.fmoney = item.money / item.count;
							item.fmoney = item.fmoney.toFixed(2);
						});
						if (order.order_type == 3) {
							order.money = Number(order.money) - Number(order.distribution_fee);
							order.freight = order.distribution_fee.toFixed(2);
						}
						let allprice = Number(order.freight) + Number(order.money);
						order.allprice = allprice.toFixed(2);
					});
					//设置列表数据
					if (page.num == 1) this.goods = []; //如果是第一页需手动制空列表
					this.goods = this.goods.concat(list); //追加新数据
					//
					this.$store.commit('settouch', 'Reset');
					// if (list.length < page.size) {

					// 	this.showCommon = true;
					// 	this.$refs.guesslike.init()
					// 	// this.$store.commit('settouch', '1');
					// }
				},
				e => {
					uni.showModal({
						title: '提示',
						content: e,
						showCancel: false
					});
					this.mescroll.endErr();
				}
			);
		},
		logincallback() {
			if (e.islogin) {
				this.downCallback();
			}
		},
		topay(item) {
			let list = [];
			list.push(item.order_no);
			list = list.join(',');
			let money = item.allprice;

			uni.navigateTo({
				url: 'pay?total=' + money + '&list=' + list
			});
		},
		goDetail(obj) {
			if (obj.money_type == 1) {
				this.Jumpurl({
					link: '../order/orderdetail?type=4&id=' + obj.id,
					type: 1,
					login_type: 2
				});
			} else {
				this.Jumpurl({
					link: '/pointsmallpage/order/order?id=' + obj.id,
					type: 1,
					login_type: 2
				});
			}
		},
		togoods(id, type) {
			if (type == 1) {
				this.Jumpurl({
					type: 2,
					id: id
				});
			} else {
				this.Jumpurl({
					type: 1,
					link: '../goods/detail?id=' + id
				});
			}
		},
		cancelOrder(id) {
			const _this = this;
			uni.showModal({
				title: '是否确认取消？',
				success(res) {
					if (res.confirm) {
						const param = {
							id: id
						};
						http('/order/orderDel/' + id, {}, 'DELETE').then(
							res => {
								if (res.statusCode == 201) {
									_this.downCallback();
								} else {
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									});
								}
							},
							e => {
								uni.showModal({
									title: '提示',
									content: e,
									showCancel: false
								});
							}
						);
					}
				}
			});
		},
		delOrder(id) {
			const _this = this;
			const param = {
				id: id
			};
			http('/order/delete', param, 'POST').then(
				res => {
					const json = res.data;
					if (json.code == 0) {
						_this.downCallback();
					} else {
						uni.showModal({
							title: '提示',
							content: json.msg,
							showCancel: false
						});
					}
				},
				e => {
					uni.showModal({
						title: '提示',
						content: e,
						showCancel: false
					});
				}
			);
		},
		refundOrder(obj) {
			uni.navigateTo({
				url: '/pages/mine/order/detail?id=' + obj.id + '&refund=' + obj.refundState
			});
		},
		confirmOrder(obj) {
			const _this = this;
			uni.showModal({
				title: '是否确认收货？',
				success(res) {
					if (res.confirm) {
						let orderCommodity = obj.orderCommodity;
						let cids = [];
						orderCommodity.forEach(a => {
							if (a.status == 3) {
								cids.push(a.ocid);
							}
						});
						let param = {
							id: obj.id,
							cids: JSON.stringify(cids)
						};
						http('/order/confirm', param, 'PUT').then(res => {
							if (res.statusCode == 200) {
								_this.downCallback();
							} else {
							}
						});
					}
				}
			});
		},
		accMul(arg1, arg2) {
			var m = 0,
				s1 = arg1.toString(),
				s2 = arg2.toString();
			try {
				m += s1.split('.')[1].length;
			} catch (e) {}
			try {
				m += s2.split('.')[1].length;
			} catch (e) {}
			return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
		}
	}
};
</script>
<style lang="scss">
.uni-block-list {
	// padding-top: 100rpx !important;

	.order {
		padding-top: 240rpx;
		align-items: center;

		image {
			width: 50%;
			height: 300rpx;
			margin-bottom: 100rpx;
		}

		text {
			color: #a9a9a9;
			font-size: 26rpx;
		}
	}

	.list {
		.item {
			width: 710rpx;
			margin-left: 20rpx;
			padding: 20rpx 32rpx;
			border-radius: 12rpx;

			.head {
				padding: 0 0 20rpx 0;
			}

			.body {
				padding: 20rpx 0;

				.img_box {
					width: 140rpx;
					height: 140rpx;
					margin-right: 30rpx;
					border-radius: 8rpx;
				}

				.detail {
					flex: 1;
					justify-content: space-between;
					height: 130rpx;
					.group_div {
						background: var(--main-color);
						color: #fff !important;
						padding: 2rpx 5rpx;
						border-radius: 10rpx;
						margin-right: 5rpx;
					}
					.title {
						justify-content: space-between;
						font-size: 36rpx;
						height: 130rpx;
						width: 320rpx;

						.skuvalue {
							color: #999;
							font-size: 26rpx;
						}
					}

					.price {
						align-self: flex-start;
						display: flex;
						flex-direction: column;
						line-height: 1;

						text:first-child {
							color: #000;
						}

						.num {
							margin-top: 20rpx;
							text-align: right;
							color: #999999;
							font-size: 24rpx;
						}
					}
				}
			}

			.bottom {
				padding: 10rpx 0;

				.left {
					width: 100%;
					justify-content: flex-end;

					.price {
						font-weight: bold;
						margin-right: 16rpx;
					}
				}

				.right {
					width: 100%;
					justify-content: flex-end;

					.number {
						font-weight: bold;
					}

					.price {
						font-weight: bold;
					}
				}
			}

			.button {
				justify-content: flex-end;
				margin-top: 18rpx;

				text {
					display: inline-block;
					text-align: center;
					font-size: 24rpx;
					line-height: 50rpx;
					// padding: 12rpx 0;

					width: 150rpx;
					border-radius: 30rpx;
					height: 50rpx;
				}

				/*cancel：取消*/
				.pay {
					color: var(--main-color);
					margin-left: 20rpx;
					border: 2rpx solid var(--main-color);
				}

				.finish {
					color: #ffffff;
					margin-left: 20rpx;
				}

				.success {
					color: #ffffff;
					margin-left: 20rpx;
				}

				.cancel {
					border: 1px solid #878787;
				}

				.refund {
					border: 1px solid #878787;
				}

				.del {
					border: 1px solid #878787;
				}

				.state {
					color: #b00606;
					width: 210rpx;
					text-align: right;
				}
			}
		}
	}
}
</style>
